package zhuyun.code.launcheractivity.view.fragment

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import kotlinx.android.synthetic.main.fragment_follow_dynamic.*
import kotlinx.android.synthetic.main.fragment_follow_dynamic.view.*

import zhuyun.code.launcheractivity.R
import zhuyun.code.launcheractivity.model.Constants
import zhuyun.code.launcheractivity.model.NetworkRequest
import zhuyun.code.launcheractivity.model.ResponseDataHandler
import zhuyun.code.launcheractivity.model.data.BaseResponse
import zhuyun.code.launcheractivity.model.data.CountryBean
import zhuyun.code.launcheractivity.model.data.FollowMainBean
import zhuyun.code.launcheractivity.model.data.PostDynamicListData
import zhuyun.code.launcheractivity.model.data.interfaces.SelectTextItem
import zhuyun.code.launcheractivity.view.MainActivity
import zhuyun.code.launcheractivity.view.ProductListSearchActivity
import zhuyun.code.launcheractivity.view.adapter.BaseQuickAdapter
import zhuyun.code.launcheractivity.view.adapter.FollowDynamicAdapter
import zhuyun.code.launcheractivity.view.adapter.SelectTextListAdapter
import kotlin.math.ceil


/**
 * A simple [Fragment] subclass.
 * Use the [FollowDynamicFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class FollowDynamicFragment : Fragment(),SelectTextListAdapter.OnItemClickListener,View.OnClickListener {

    private var mContentView:View? = null
    private lateinit var followDynamicAdapter:FollowDynamicAdapter
    private val followDynamicList = mutableListOf<PostDynamicListData>()

    //国家
    private lateinit var countryListAdapter:SelectTextListAdapter
    private val countryList = mutableListOf<SelectTextItem>()

    //职业
    private lateinit var careerListAdapter:SelectTextListAdapter
    private val careerList = mutableListOf<SelectTextItem>()


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        if(mContentView == null){
            mContentView = inflater.inflate(R.layout.fragment_follow_dynamic, container, false);
        }
        initViews()
        initData()
        return mContentView

    }

    private fun initViews(){

        //动态列表
        followDynamicAdapter = FollowDynamicAdapter(activity!!,followDynamicList)
        mContentView!!.rootView.recyclerView.layoutManager = LinearLayoutManager(activity)
        mContentView!!.rootView.recyclerView.adapter = followDynamicAdapter
        followDynamicAdapter.setOnLoadMoreListener(object :BaseQuickAdapter.RequestLoadMoreListener{
            override fun onLoadMoreRequested() {
                mContentView!!.rootView.recyclerView.postDelayed({
                    if(page <= totalPage){
                        getData()
                    } else {
                        followDynamicAdapter.loadMoreEnd()
                    }
                },500)

            }

        })
        mContentView!!.rootView.refreshLayout.setOnRefreshListener {
            page = 1
            getData()
        }

        //国家
        countryListAdapter = SelectTextListAdapter(activity!!,countryList)
        mContentView!!.rootView.rvCountry.layoutManager = LinearLayoutManager(activity)
        mContentView!!.rootView.rvCountry.adapter = countryListAdapter
        countryListAdapter.onItemClickListener = this


        //职业
        careerListAdapter = SelectTextListAdapter(activity!!,careerList)
        mContentView!!.rootView.rvCareer.layoutManager = LinearLayoutManager(activity)
        mContentView!!.rootView.rvCareer.adapter = careerListAdapter
        careerListAdapter.onItemClickListener = this

        mContentView!!.rootView.cvCountry.setOnClickListener(this)
        mContentView!!.rootView.cvCareer.setOnClickListener(this)

        //搜索框
        mContentView!!.rootView.cvKeyword.setOnClickListener(this)

    }

    private var mainClassId = 0
    private var countryId = 0
    private var keyWords = ""
    private var page = 1
    private var pageSize = 10

    private fun initData(){
        NetworkRequest.getCountry(object :ResponseDataHandler<List<CountryBean>>(){
            override fun onSuccess(
                resultData: List<CountryBean>?,
                baseResponse: BaseResponse<List<CountryBean>>
            ) {
                resultData?.let {
                    countryList.clear()
                    val countryBean = CountryBean(CountryId = 0,CountryName = "国家")
                    countryList.add(0,countryBean)
                    countryList.addAll(it)
                    countryListAdapter.selectedItemId = countryId
                    countryListAdapter.notifyDataSetChanged()
                }


            }

            override fun onFailure(errMsg: String?) {

            }

        })
        NetworkRequest.getFollowMain(0,1,40,object :ResponseDataHandler<List<FollowMainBean>>(){
            override fun onSuccess(
                resultData: List<FollowMainBean>?,
                baseResponse: BaseResponse<List<FollowMainBean>>
            ) {
                resultData?.let {
                    careerList.clear()
                    val followMainBean = FollowMainBean(MainClassId = 0,MainClassName = "明星类别")
                    careerList.add(0,followMainBean)
                    careerList.addAll(it)
                    careerListAdapter.selectedItemId = mainClassId
                    careerListAdapter.notifyDataSetChanged()

                }

            }

            override fun onFailure(errMsg: String?) {

            }

        })
        getData()


    }

    override fun onItemClick(item: SelectTextItem) {
        if(item is CountryBean){
            countryId = item.CountryId
            tvCountry.text = item.CountryName
            countryListAdapter.selectedItemId = countryId
            countryListAdapter.notifyDataSetChanged()
            rvCareer.visibility = View.GONE
            rvCountry.visibility = View.GONE
        } else if(item is FollowMainBean){
            mainClassId = item.MainClassId
            tvCareer.text = item.MainClassName
            careerListAdapter.selectedItemId = mainClassId
            careerListAdapter.notifyDataSetChanged()
            rvCareer.visibility = View.GONE
            rvCountry.visibility = View.GONE
        }
        page = 1
        getData()
    }

    private var totalPage = 0

    private fun getData(){
        NetworkRequest.getPostDynamic(keyWords,mainClassId,countryId,1,page,pageSize,object :ResponseDataHandler<List<PostDynamicListData>>(){
            override fun onSuccess(
                resultData: List<PostDynamicListData>?,
                baseResponse: BaseResponse<List<PostDynamicListData>>
            ) {
                resultData?.let {
                    if(refreshLayout.isRefreshing){
                        refreshLayout.isRefreshing = false
                    }
                    if(page == 1){
                        followDynamicList.clear()
                    }
                    followDynamicList.addAll(resultData)
                    followDynamicAdapter.notifyDataSetChanged()
                    followDynamicAdapter.loadMoreComplete()
                    val rowCount = baseResponse.RowCount
                    totalPage = ceil(rowCount/pageSize.toFloat()).toInt()
                    page++
                }

            }

            override fun onFailure(errMsg: String?) {

            }

        })
    }

    private var isCountryShow = false
    private var isCareerShow = false

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.cvCountry->{
                rvCareer.visibility = View.GONE
                isCareerShow = false
                if(isCountryShow){
                    rvCountry.visibility = View.GONE
                } else {
                    rvCountry.visibility = View.VISIBLE
                }
                isCountryShow = !isCountryShow
            }
            R.id.cvCareer->{
                rvCountry.visibility = View.GONE
                isCountryShow = false
                if(isCareerShow){
                    rvCareer.visibility = View.GONE
                } else {
                    rvCareer.visibility = View.VISIBLE
                }
                isCareerShow = !isCareerShow
            }
            R.id.cvKeyword->{
                startActivityForResult(Intent(activity,ProductListSearchActivity::class.java), 101)
            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(resultCode == Activity.RESULT_OK){
            when(requestCode){
                101->{
                    keyWords = data!!.getStringExtra(Constants.KeyWord)!!
                    tvSearch.text = keyWords
                    page = 1
                    getData()
                }


            }
        }
    }
}
